package com.financing.wap.web.controller.api;


import java.io.IOException;
import java.io.StringWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;  
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.dto.BaseDTO;
import com.financing.wap.dto.ProjectCompanyDTO;
import com.financing.wap.service.IProductService;
import com.financing.wap.service.IProjectCompanyService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.PropertiesUtils;
import com.financing.wap.web.controller.BaseController;

/**
 * 网站地图
 * @author jiayao.cao
 *
 */
@Controller("siteMapController")
@RequestMapping("/api")
public class SiteMapController extends BaseController { 
	 Logger logger = Logger.getLogger(getClass());
	 
     //链接地址
     private static final String NODE_LOC = "loc";
     
     //更新时间
     private static final String NODE_LASTMOD = "lastmod";
     
     //更新频率
     private static final String NODE_CHANGEFREQ = "changefreq";
     
     //索引优先权
     private static final String NODE_PRIORITY = "priority";
     
     private static String NODE_PROJECT_URL = PropertiesUtils.getValue("siteMap.Project.url");
     
     private static String NODE_FUND_URL = PropertiesUtils.getValue("siteMap.Fund.url");
     
     private static String NODE_ACTIVITIES_URL = PropertiesUtils.getValue("siteMap.Activities.url");
     
     private static String NODE_PROJECTCOMPANY_URL = PropertiesUtils.getValue("siteMap.ProjectCompany.url");
     
     private static String NODE_STATICPAGE_URL = PropertiesUtils.getValue("siteMap.StaticPage.url");
     
     @Autowired
 	 private IProductService productService;
     
     @RequestMapping("generateSiteMapXML.xml")
     @ResponseBody
     public String createSiteMapXMl(HttpServletResponse response){
    	 
    	 	Document document = DocumentHelper.createDocument();
    	 	Element urlsetElement = document.addElement("urlset");
	         urlsetElement.addAttribute("xmlns ", "http://www.sitemaps.org/schemas/sitemap/0.9"); 
	         
	         //生成产品sitemap节点
	         this.createProjectElement(urlsetElement);
	         
	         //生成私募sitemap节点
	         this.createFundElement(urlsetElement);
	         
	         //生成活动sitemap节点
	         this.createActivitiesElement(urlsetElement);
	         
	         //生成信託公司sitemap节点
	         this.createProjectCompanyElement(urlsetElement);
	         
	         //生成静态页面sitemap节点
	         this.createStaticPageElement(urlsetElement);
	         
	         
	         
	         StringWriter strWtr = new StringWriter();
	         OutputFormat format = OutputFormat.createPrettyPrint();
	         format.setEncoding("UTF-8");
	         XMLWriter xmlWriter =new XMLWriter(strWtr, format);
	     try {
	         xmlWriter.write(document);
         } catch (Exception e) {
        	 logger.error("读取异常!", e);
		}
         
         String strXml = strWtr.toString();
         logger.info("SiteMap结果===========>"+strXml);
         return strWtr.toString();
     }
     
     //生成产品节点
     private void createProjectElement(Element element){
    	 List projectList = productService.querySiteMapProjectList();
    	 if(projectList != null){
    		 addUrlElement(element, projectList,NODE_PROJECT_URL);
    	 }
     }
     
     //生成私募节点
     private void createFundElement(Element element){
    	 List projectList = productService.querySiteMapFundList();
    	 if(projectList != null){
    		 addUrlElement(element, projectList,NODE_FUND_URL);
    	 }
     }
     
     //生成活动节点
     private void createActivitiesElement(Element element){
    	 List projectList = productService.querySiteMapActivitiesList();
    	 if(projectList != null){
    		 addUrlElement(element, projectList,NODE_ACTIVITIES_URL);
    	 }
     }
     
     //生成信托公司节点
     private void createProjectCompanyElement(Element element){
    	 List list = productService.querySiteMapProjectCompanyList();
    	 if(list != null){
    		 addUrlElement(element, list,NODE_PROJECTCOMPANY_URL);
    	 }
     }
     
     //静态页面节电
     private void createStaticPageElement(Element element){
    	 String pageStr = PropertiesUtils.getValue("siteMap.StaticPage.urls");
    	 String[] pages = pageStr.split("\\|\\|");
    	 List<Map> list = new ArrayList<Map>();
    	 for (String page : pages) {
    		 Map<String, String> map = new HashMap<String, String>();
    		 map.put(NODE_LOC.toUpperCase(), page);
    		 map.put(NODE_LASTMOD.toUpperCase(),DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
    		 list.add(map);
		}
    	 
    	 if(list != null){
    		 addUrlElement(element, list,NODE_STATICPAGE_URL);
    	 }
     }
     
     private void addUrlElement(Element element,List<Map> List,String url){
     	for (Map map : List) {
     		String loc = map.get(NODE_LOC.toUpperCase()).toString();
     		String lastmod = map.get(NODE_LASTMOD.toUpperCase()).toString();
     		if(loc != null && lastmod != null){
 	    		Element urlElement = element.addElement("url");
 	    		urlElement.addElement(NODE_LOC).setText(url+loc);
 	    		urlElement.addElement(NODE_LASTMOD).setText(lastmod);
     		}
 		}
     }
    
 }
 